<?xml version="1.0" ?>
<project name="structured" default="deploy">
<!-- The MIT License (MIT) Copyright (c) 2011 Alessandro Zifiglio -->
  <loadfile failonerror="no" srcFile="version.txt" property="release.version">
    <filterchain>
      <striplinebreaks/>
    </filterchain>
  </loadfile>
  <!-- all values pointing to files and folders are case sensitive -->
  <!-- dist-->
  <property name="release.filename" value="projectname.${release.version}" />
  <property name="dist.dir" value="dist/${release.filename}" />
  <property name="dist.dir.scripts" value="${dist.dir}/scripts" />
  <property name="dist.dir.css" value="${dist.dir}/css" />
  <property name="dist.dir.demo" value="${dist.dir}/demo" />
  <property name="dist.dir.docs" value="${dist.dir}/docs" />
  
  <!-- src-->
  <property name="src.dir" value="src/com/projectname" />
  <property name="src.dir.scripts" value="${src.dir}/scripts" />
  <property name="src.dir.css" value="${src.dir}/css" />
  <property name="src.dir.demo" value="${src.dir}/demo" />
  <property name="src.dir.docs" value="${src.dir}/docs" />
  
  <!-- compiler tools-->
  <property name="tools.dir" value="../tools" />
  <property name="googleclosure" value="${tools.dir}/compiler.jar" />
  <property name="closureoptimizations" value="ADVANCED_OPTIMIZATIONS" /><!-- ADVANCED_OPTIMIZATIONS or SIMPLE_OPTIMIZATIONS-->
  <property name="js.doc" value="${tools.dir}/jsdoc-toolkit/jsrun.jar" />
  <property name="js.doc.run" value="${tools.dir}/jsdoc-toolkit/app/run.js" />
  <property name="js.doc.templates" value="${tools.dir}/jsdoc-toolkit/templates/jsdoc" />
  <property name="yuicompressor" value="${tools.dir}/yuicompressor-2.4.2.jar" />
  
  <!-- ignore files, use it to provide conditional assembly of src files. an empty string assembles all files-->
  <property name="css.exclude" value=""/><!-- a comma separated list of css file names to exclude from concatenation -->
  <property name="scripts.exclude" value=""/><!-- a comma separated list of scripts to exlude from concatenation for eg: we have utilities that use jquery and ones that dont -->
  <property name="docs.exclude" value=""/><!-- a comma separated list of script file names (external libs we depend on) to exclude from documentation eg: spryeffects.debug.js-->
  
  <property name="target.zip" value="${dist.dir}/${release.filename}.zip"/>
  <!-- we are using tasks available in ant-contrib, a separate project: 
    Tasks used so far : 
    1) For task -->
  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="${tools.dir}/ant-contrib-0.6.jar"/>
    </classpath>
  </taskdef>
  <target name="jsdocs">
    <for param="root">
      <path>
        <!-- exclude the root scripts folder, note includes param -->
        <dirset dir="${src.dir.scripts}" includes="**/*">
          <depth max="0"/>
        </dirset>
      </path>
      <sequential>
        <echo message="running js-doc ${js.doc}" />
		<propertyregex override="yes" property="rootfoldername" input="@{root}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
        <apply executable="java">
          <arg line="-jar" />
          <arg path="${js.doc}" />
          <arg path="${js.doc.run}" />
          <arg value="--allfunctions" />
          <arg line="--template=${js.doc.templates}" />
          <arg line="--directory=${src.dir.docs}\${rootfoldername}" />
          <fileset dir="@{root}" includes="${rootfoldername}.debug.js" excludes="${docs.exclude}, *.min.js" />
        </apply>
        <echo message="finished documenting javascript in @{root}\${rootfoldername}.debug.js" />
      </sequential>
    </for>
  </target>
  
  <target name="concatjs">
    <for param="root">
      <path>
        <!-- exclude the root scripts folder, note includes param -->
        <dirset dir="${src.dir.scripts}" includes="**/*">
          <depth max="0"/>
        </dirset>
      </path>
      <sequential>
        <echo message="assembling *.js files in @{root} directory" />
        <propertyregex override="yes" property="rootfoldername" input="@{root}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
        <delete>
          <fileset dir="@{root}\" includes="*.min.js, *.debug.js" />
        </delete>
        <for param="child">
          <path>
            <dirset dir="@{root}" />
          </path>
          <sequential>
            <propertyregex override="yes" property="foldername" input="@{child}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
            <echo message="assembling *.js files into unified file : @{root}\${rootfoldername}.debug.js" />
            <concat destfile="@{root}\${rootfoldername}.debug.js" append="yes" fixlastline="yes">
              <fileset dir="@{child}" includes="*.js" excludes="${scripts.exclude}" />
            </concat>
          </sequential>
        </for>
		<echo message="creating copy of @{root}\${rootfoldername}.debug.js in dist"/>
        <copy todir="${dist.dir.scripts}">
          <fileset file="@{root}\${rootfoldername}.debug.js" />
        </copy>
        <echo message="*.js files in  @{root} concatenation complete" />
        <echo message="completed assembling scripts"/>
      </sequential>
    </for>
  </target>
  
  <target name="minifyjs" depends="concatjs">
    <for param="root">
      <path>
        <!-- exclude the root scripts folder, note includes param -->
        <dirset dir="${src.dir.scripts}" includes="**/*">
          <depth max="0"/>
        </dirset>
      </path>
      <sequential>
        <propertyregex override="yes" property="rootfoldername" input="@{root}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
        <echo message="begin compacting scripts..." />
        <echo message="using compiler located at ${googleclosure}" />
		 <if>
			<not>
				<available file="@{root}\${rootfoldername}.extern.thirdparty.in"/>
			</not>
			<then>
				<echo message="creating a default extern file because one does not exist" />
				<echo file="@{root}\${rootfoldername}.extern.thirdparty.in">/*use this file to add all your third party library externs(the bits in your api you do not want renamed by the closure compiler)*/</echo> 
			</then>
		</if>
		<if>
			<not>
				<available file="@{root}\${rootfoldername}.extern.local.in"/>
			</not>
			<then>
				<echo message="creating a default local extern file because one does not exist" />
				<echo file="@{root}\${rootfoldername}.extern.local.in">/*use this file for your main library externs(the bits in your api you do not want renamed by the closure compiler)*/</echo> 
			</then>
		</if>
        <apply executable="java">
          <arg line="-jar" />
          <arg path="${googleclosure}" />
		  <arg line="--compilation_level=${closureoptimizations}" />
		  <arg line="--warning_level=VERBOSE" />
		  <arg line="--externs=@{root}\${rootfoldername}.extern.local.in" /><!-- a file containing prop/method names you don't want renamed-->
		  <arg line="--externs=@{root}\${rootfoldername}.extern.thirdparty.in" /><!-- a file containing methods in third party code that you don't want renamed-->
          <arg value="--js" />
          <srcfile/>
          <arg value="--js_output_file" />
          <targetfile />
          <fileset dir="@{root}" includes="*.debug.js" excludes="${scripts.exclude}" />
          <mapper type="glob" from="*.debug.js" to="${dist.dir.scripts}/*.min.js" />
        </apply>
		<echo message="completed minifying javascript in ${dist.dir.scripts} directory" />
        <copy todir="@{root}">
          <fileset file="${dist.dir.scripts}/${rootfoldername}.min.js" />
        </copy>
		<echo message="completed copying ${dist.dir.scripts}/${rootfoldername}.min.js into @{root} directory"/>
      </sequential>
    </for>
  </target>

  <target name="concatcss">
    <for param="root">
      <path>
        <!-- exclude the root scripts folder, note includes param -->
        <dirset dir="${src.dir.css}" includes="**/*">
          <depth max="0"/>
        </dirset>
      </path>
      <sequential>
        <echo message="assembling *.css files in @{root} directory" />
        <propertyregex override="yes" property="rootfoldername" input="@{root}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
        <delete>
          <fileset dir="@{root}\" includes="*.min.css, *.debug.css" />
        </delete>

        <for param="child">
          <path>
            <dirset dir="@{root}" />
          </path>
          <sequential>
            <propertyregex override="yes" property="foldername" input="@{child}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
            <echo message="assembling *.css files into unified file : @{root}\${rootfoldername}.debug.css" />
            <concat destfile="@{root}\${rootfoldername}.debug.css" append="yes" fixlastline="yes">
              <fileset dir="@{child}" includes="*.css" excludes="${css.exclude}" />
            </concat>
          </sequential>
        </for>
        <echo message="completed assembling css" />
      </sequential>
    </for>
  </target>
  
  <target name="minifycss" depends="concatcss">
    <for param="root">
      <path>
        <!-- exclude the root scripts folder, note includes param -->
        <dirset dir="${src.dir.css}" includes="**/*">
          <depth max="0"/>
        </dirset>
      </path>
      <sequential>
        <propertyregex override="yes" property="rootfoldername" input="@{root}" regexp="^(.+)\\([^\\]+)$" replace="\2"/>
        <echo message="creating copy of @{root}\${rootfoldername}.debug.css"/>
        <copy todir="${dist.dir.css}">
          <fileset file="@{root}\${rootfoldername}.debug.css"/>
        </copy>
        
        <echo message="begin compacting..." />
        <echo message="using compiler located at ${yuicompressor}" />
        <apply executable="java">
          <arg line="-jar" />
          <arg path="${yuicompressor}" />
          <srcfile />
          <arg line="-o" />
          <targetfile />
          <arg line="--type css" />
          <arg line="--charset utf-8" />
          <arg line="-v" />
          <fileset dir="@{root}" includes="*.debug.css" excludes="${css.exclude}" />
          <mapper type="glob" from="*.debug.css" to="${dist.dir.css}/*.min.css" />
        </apply>
		<echo message="completed minifying css in ${dist.dir.css} directory" />
        <copy todir="@{root}">
          <fileset file="${dist.dir.css}/${rootfoldername}.min.js" />
        </copy>
		<echo message="completed copying minified ${dist.dir.css}/${rootfoldername}.min.js into @{root} folder"/>
      </sequential>
    </for>
  </target>
  
  <target name="copy-dir">
    <echo message="copying ${src.dir.demo} to ${dist.dir}" />
    <echo message="copying ${src.dir.docs} to ${dist.dir}" />
    <copy todir="${dist.dir.demo}">
      <fileset dir="${src.dir.demo}"/>
    </copy>
    <copy todir="${dist.dir.docs}">
      <fileset dir="${src.dir.docs}"/>
    </copy>
  </target>
  
  <target name="create-folder-structure" depends="clean">
    <echo message="making project directory ${src.dir}"/>
    <mkdir dir="${src.dir}" />

    <echo message="making ${src.dir.scripts} directory" />
    <mkdir dir="${src.dir.scripts}" />

    <echo message="making ${src.dir.css} directory" />
    <mkdir dir="${src.dir.css}" />

    <echo message="making ${src.dir.demo} directory" />
    <mkdir dir="${src.dir.demo}" />
    
    <echo message="making ${src.dir.docs} directory" />
    <mkdir dir="${src.dir.docs}" />

    <echo message="making ${dist.dir} directory" />
    <mkdir dir="${dist.dir}" />

    <echo message="making ${dist.dir.scripts} directory" />
    <mkdir dir="${dist.dir.scripts}" />

    <echo message="making ${dist.dir.css} directory" />
    <mkdir dir="${dist.dir.css}" />
  </target>

  <target name="deploy" depends="create-folder-structure, concatcss, minifycss, concatjs, jsdocs, minifyjs, copy-dir">
    <echo message="creating ${target.zip}" />
    <zip destFile="${target.zip}">
      <zipfileset dir="${dist.dir}"  includes="**/*" />
    </zip>
  </target>
  
  <target name="clean">
    <delete failonerror="false" verbose="true">
      <fileset dir="${dist.dir.scripts}" includes="*.js"/>
    </delete>
    <delete failonerror="false" verbose="true">
      <fileset dir="${dist.dir.css}" includes="*.css"/>
    </delete>
    <delete failonerror="false" verbose="true">
      <fileset dir="${dist.dir.demo}" includes="**/*"/>
    </delete>
    <delete failonerror="false" verbose="true">
      <fileset dir="${dist.dir.docs}" includes="**/*"/>
    </delete>
    <delete failonerror="false" verbose="true">
      <fileset file="${target.zip}"/>
    </delete>
  </target>
  <!-- 
    References for each type/task used : 
     http://ant.apache.org/manual/Types/mapper.html
     http://ant.apache.org/manual/Tasks/apply.html
     http://ant.apache.org/manual/Tasks/concat.html
     http://ant.apache.org/manual/Types/dirset.html
     http://ant.apache.org/manual/Types/fileset.html
     http://ant.apache.org/manual/Tasks/delete.html
     http://ant.apache.org/manual/Tasks/mkdir.html
     http://ant.apache.org/manual/Tasks/zip.html
     http://ant.apache.org/manual/Types/zipfileset.html
     http://ant.apache.org/manual/Tasks/condition.html
     http://ant-contrib.sourceforge.net/tasks/tasks/if.html
     http://ant-contrib.sourceforge.net/tasks/tasks/for.html
-->
</project>